<template>
  <el-dialog width="500px" title="扣除押金" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <el-form v-loading='formLoading' ref="ruleForm" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="可扣除押金：">
        <p>￥{{ depositNum }}</p>
      </el-form-item>
      <el-form-item prop="type" label="扣款类型：">
        <el-radio-group v-model="form.type">
          <el-radio label="1">预授权扣款</el-radio>
          <el-radio label="2">银行卡扣款</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="cusDeposit" label="扣除押金：">
        <el-input v-model="form.cusDeposit" placeholder="请输入扣除押金" type="number" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { deductDeposit, getSurplusDeposit } from '@/api/order'
export default {
  // props: ["orderId"],
  props: {
    orderId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      form: {
        cusDeposit: '',
        type: '1'
      },
      depositNum: '',
      dialogVisible: false,
      rules: {
        cusDeposit: [{ required: true, message: '请输入扣除押金', trigger: 'change' }],
      },
      formLoading: false
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.getSurplusDepositMethod()
      }
    },
  },
  methods: {
    getSurplusDepositMethod() {
      this.formLoading = true
      getSurplusDeposit(this.orderId).then((res) => {
        this.depositNum = res.rest_amount || ''
      }).finally(()=>{
        this.form.type = '1'
        this.form.cusDeposit = ''
        this.formLoading = false
      })
    },
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          let isCheck = this.form.type === '1'
          if (isCheck && Number(this.depositNum) < Number(this.form.cusDeposit)) {
            this.$message({
              message: '扣款金额不能大于可扣除金额',
              type: 'warning',
            })
            return
          }
          deductDeposit({
            orderId: this.orderId,
            amount: this.form.cusDeposit,
            type: this.form.type
          }).then((res) => {
            this.dialogVisible = false
            this.$emit('getList', true)
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error',
          })
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
a,
a:focus,
a:hover {
  color: $mainBlue;
}
</style>
